package com.whfc.wxmp.api.quality.controller;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.*;
import com.whfc.common.util.AssertUtil;
import com.whfc.wxmp.annotation.CurrentMsUser;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.entity.dto.statistics.CheckDetailDTO;
import com.whfc.entity.dto.statistics.CheckStatisticsDTO;
import com.whfc.entity.param.check.CheckSearchParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.quality.param.QualityCheckLogParam;
import com.whfc.quality.param.QualityCheckParam;
import com.whfc.wxmp.api.quality.service.WxQualityCheckService;
import com.whfc.wxmp.api.quality.service.WxQualityIssueService;
import com.whfc.wxmp.api.quality.service.WxQualityPartService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @ClasssName WxCheckController
 * @Description 质量问题接口
 * @Author hw
 * @Date 2020/7/30 10:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/mp/api/quality")
public class WxQualityCheckController extends AbstractBaseController {

    @Autowired
    private WxQualityCheckService wxQualityCheckService;

    @Autowired
    private WxQualityIssueService wxQualityIssueService;

    @Autowired
    private WxQualityPartService wxQualityPartService;


    @PostMapping("/check/add")
    public Result add(@CurrentMsUser SysUser user, @Validated @RequestBody QualityCheckParam checkParam) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量问题上报接口", RequestConetxtUtil.getParameters());
        AssertUtil.isTrue(checkParam.getRectifyDuration().before(new Date()),"整改期限不能设置在当前时间之前");
        wxQualityCheckService.add(checkParam, user);
        return ResultUtil.success();
    }

    @GetMapping("/check/user/list")
    public Result getUserList(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId,
                              @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员信息接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(new ListData<>(wxQualityCheckService.sysUserList(deptId, keyword)));
    }

    @PostMapping("/check/log")
    public Result addLog(@CurrentWxUser WxUser user, @Validated @RequestBody QualityCheckLogParam qualityCheckLogParam) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量问题流程处理接口", RequestConetxtUtil.getParameters());
        wxQualityCheckService.addCheckLog(qualityCheckLogParam, user.getPhone());
        return ResultUtil.success();
    }

    @PostMapping("/uploadImg")
    public Result uploadImg(@CurrentWxUser WxUser user, MultipartFile file) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量管理图片上传", RequestConetxtUtil.getParameters());
        String imgUrl = wxQualityCheckService.uploadImg(file);
        if (StringUtils.isBlank(imgUrl)) {
            return ResultUtil.failure("图片上传失败");
        }
        return ResultUtil.success(imgUrl);
    }

    @Deprecated
    @GetMapping("/check/list")
    public Result list(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "type", required = false) String type,
                       @RequestParam(value = "state", required = false) String state,
                       @RequestParam(value = "urgency", required = false) String urgency,
                       @RequestParam(value = "overdue", required = false) String overdue,
                       @RequestParam(value = "pageNum", required = false) Integer pageNum,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量问题上报列表查询接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(wxQualityCheckService.getCheckList(deptId, type, pageNum, pageSize, user.getPhone(), state, urgency, overdue, startTime, endTime));
    }

    @GetMapping("/check/desc")
    public Result desc(@CurrentWxUser WxUser user, @RequestParam("checkId") Integer checkId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量问题上报详情查询接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(wxQualityCheckService.getCheck(checkId, user));
    }

    @GetMapping("/check/statistics")
    public Result statistical(@CurrentWxUser WxUser user,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM") Date time) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "质量问题上报统计接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(wxQualityCheckService.statistical(deptId, time));
    }

    @GetMapping("/part/list")
    public Result partList(@CurrentWxUser WxUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询检测部位接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(new ListData<>(wxQualityPartService.partList(deptId, keyword)));
    }

    @GetMapping("/issue/type/list")
    public Result issueTypeList(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询质量问题类别接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(new ListData<>(wxQualityIssueService.issueTypeList(deptId)));
    }

    @GetMapping("/issue/list")
    public Result issueList(@CurrentWxUser WxUser user, @RequestParam("issueTypeId") Long issueTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询质量问题明细接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(new ListData<>(wxQualityIssueService.issueList(issueTypeId)));
    }

    @GetMapping("/issue/history/list")
    public Result issueHistoryList(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId, @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询质量历史问题明细接口", RequestConetxtUtil.getParameters());
        return ResultUtil.success(new ListData<>(wxQualityIssueService.issueHistoryList(deptId, pageSize)));
    }

    /**
     * 2.0.0质量管理-顶栏统计
     *
     * @return
     */
    @PostMapping("/check/data/statistics")
    public R<CheckStatisticsDTO> getCheckStatistics(@Valid @RequestBody CheckSearchParam param) {
        logger.info("我的质量管理数据统计(顶栏使用):param={},userId={}", param, getUserId());
        AssertUtil.isTrue(param.getDeptId() == null, "项目ID不能为空");
        param.setUserId(getUserId());
        CheckStatisticsDTO res = wxQualityCheckService.getCheckStatistics(param);
        return R.success(res);
    }

    /**
     * 2.0.0质量管理-分页
     *
     * @param param
     * @return
     */
    @PostMapping("/check/data/page")
    public R<PageVO<CheckDetailDTO>> getCheckPage(@Valid @RequestBody CheckSearchParam param) {
        logger.info("我的质量管理分页:param={},userId={}", param, getUserId());
        if (!CollectionUtils.isEmpty(param.getTypeList())) {
            param.setUserId(getUserId());
        }
        PageVO<CheckDetailDTO> res = wxQualityCheckService.getCheckPage(param);
        return R.success(res);
    }

}
